package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.PartnerApplicationData;
import com.banzhi.shop.model.CallBack.PartnerApplictionCallBack;
import com.banzhi.shop.model.IPartnerApplicationModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.utils.L;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/6/26.
 */

public class PartnerApplicationImpl implements IPartnerApplicationModel {
    @Override
    public void applyForPartnerData(String username, String mobile, String address, String userid, String imguserida, String imguseridb, final PartnerApplictionCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.CityPartnersURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        if(ASUtils.isCanUse(username)){
            params.addBodyParameter("username", username);
        }
        if(ASUtils.isCanUse(mobile)){
            params.addBodyParameter("mobile", mobile);
        }
        if(ASUtils.isCanUse(address)){
            params.addBodyParameter("address", address);
        }

        if(ASUtils.isCanUse(userid)){
            params.addBodyParameter("userid", userid);
        }
        if(ASUtils.isCanUse(imguserida)){
            params.addBodyParameter("imguserida", imguserida);
        }
        if(ASUtils.isCanUse(imguseridb)){
            params.addBodyParameter("imguseridb", imguseridb);
        }
        LogUtils.d("PartnerApplicationImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        callBack.applyForPartnerSucess(jsonobject);
                    } else {
                        callBack.applyForPartnerError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PartnerApplicationImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(93)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void getPartnerApplicationData(final PartnerApplictionCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.CityPartnersURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.d("getPartnerApplicationData",result.toString());
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        PartnerApplicationData applicationData = gson.fromJson(result, PartnerApplicationData.class);
                        callBack.getPartnerApplicationSucess(applicationData.getData());
                    }else {
                        callBack.getPartnerApplicationError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PartnerApplicationImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
            //    ToastUtils.makeToastShort("网络加载失败,请稍后再试(94)");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void changePartnerApplicationData(String username, String mobile, String address, String userid, String imguserida, String imguseridb, final PartnerApplictionCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChangeCityPartnersURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        if(ASUtils.isCanUse(username)){
            params.addBodyParameter("username", username);
        }
        if(ASUtils.isCanUse(mobile)){
            params.addBodyParameter("mobile", mobile);
        }
        if(ASUtils.isCanUse(address)){
            params.addBodyParameter("address", address);
        }

        if(ASUtils.isCanUse(userid)){
            params.addBodyParameter("userid", userid);
        }
        if(ASUtils.isCanUse(imguserida)){
            params.addBodyParameter("imguserida", imguserida);
        }
        if(ASUtils.isCanUse(imguseridb)){
            params.addBodyParameter("imguseridb", imguseridb);
        }
        LogUtils.d("changePartnerApplicationData", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                       callBack.changePartnerApplicationSucess(jsonobject);
                    } else {
                        callBack.changePartnerApplicationError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("changePartnerApplicationData", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(98)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });

    }

}

